package cn.itcast.interceptors;

import cn.itcast.config.TestProperties;
import cn.itcast.service.UserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 虎哥
 */
@Slf4j
@Component
@Data
@EnableConfigurationProperties(TestProperties.class)
public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Autowired
    private TestProperties testProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 记录日志
        log.info("【MyInterceptor】的 preHandle方法在执行！");
        // 放行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        log.info("【MyInterceptor】的 postHandle方法在执行！");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("【MyInterceptor】的 afterCompletion方法在执行！");
    }
}
